package com.qiku.controller;

import com.qiku.domin.*;
import com.qiku.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/findAllCourse", method = RequestMethod.POST)
    public ResponseResult findCourseByCondition(@RequestBody CourseVO courseVO) {
        // 查询课程
        List<Course> courseList = courseService.findCourseByCondition(courseVO);
        // 封装结果集
        ResponseResult result = null;
        if (null != courseList) {
            result = new ResponseResult(true, 200, "响应成功", courseList);
        } else {
            result = new ResponseResult(false, 200, "查询结果为空", courseList);
        }
        return result;
    }


    @RequestMapping(value = "/courseUpload", method = RequestMethod.POST)
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest req) {
        try {
            // 判断文件是否为空
            if (file.isEmpty()) {
                throw new RuntimeException("未找到图片信息...");
            }
            // 获取项目部署的路径
            String realPath = req.getServletContext().getRealPath("/");
            System.out.println(realPath);
            // ssm 项目名   获取项目名
            String projectName = req.getContextPath().substring(1);

            // 获取上传文件的真实路径
            String webappsPath = realPath.substring(0, realPath.indexOf(projectName));

            String imgPath = webappsPath + "/upload/";
            // 获取源文件名
            String filename = file.getOriginalFilename();
            // 设置新文件名 （随机字符串） 防止命名冲突   System.currentTimeMillis()  获取当前毫秒数
            String newFileName = System.currentTimeMillis() + filename;
            // 上传文件
            File filePath = new File(imgPath, newFileName);

            file.transferTo(filePath);
            // 返回结果集
            Map<String, String> map = new HashMap<>();
            map.put("fileName", newFileName);
            map.put("filePath", imgPath + newFileName);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 保存&修改课程信息
     */
    @RequestMapping(value = "/saveOrUpdateCourse", method = RequestMethod.POST)
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVO courseVO) {
        ResponseResult result = null;
        try {
            // 根据参数中是否含有id属性  判断是 新增 或者修改操作
            if (null != courseVO.getId() && 0 < courseVO.getId()) {
                // id 合法  表示修改操作
                courseService.updateCourseOrTeacher(courseVO);
                result = new ResponseResult(true, 200, "修改成功", null);
                return result;
            } else {
                // 新增 保存操作
                courseService.saveCourseOrTeacher(courseVO);
                result = new ResponseResult(true, 200, "保存成功", null);
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生异常", e);
            return result;
        }

    }

    /**
     * 根据id 查询课程信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findCourseById", method = RequestMethod.GET)
    public ResponseResult findCourseById(@RequestParam Integer id) {

        try {
            CourseVO courseVO = courseService.findCourseById(id);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", courseVO);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @RequestMapping(value = "/updateCourseStatus", method = RequestMethod.GET)
    public ResponseResult updateCourseStatus(@RequestParam Integer id, @RequestParam int status) {

        try {
            courseService.updateStatusCourse(id, status);
            // 封装最新的状态信息
            Map<String, Integer> map = new HashMap<>();
            map.put("stauts", status);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


}
